//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.utility;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {
	String modelName;
	String objectName;
	String nestedModelName;
	String nestedObjectName;
	ParsedXMLObject parsedObject;
	boolean field;
	String[] fieldNames;
	String[] nestedFieldNames;
	private String value;
	boolean parsingNestedObject;
	ParsedXMLObject parsedNestedObject;

	public XMLHandler(String modelName, String objectName, String fieldNames[]) {
		this.modelName = modelName;
		this.objectName = objectName;
		this.fieldNames = fieldNames;
		parsedObject = new ParsedXMLObject();
		value = new String();
	}

	public XMLHandler(String modelName, String objectName,
			String nestedModelName, String nestedObjectName,
			String[] fieldNames, String[] nestedFieldNames) {
		this(modelName,objectName,fieldNames);
		this.nestedModelName = nestedModelName;
		this.nestedObjectName = nestedObjectName;
		this.nestedFieldNames = nestedFieldNames;
		parsedNestedObject=new ParsedXMLObject();
	}
	
	@Override
	public void startDocument() throws SAXException {

	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.replace('-','_').equals(nestedObjectName))				
			parsingNestedObject = true;
		else if ((!localName.replace('-','_').equals(modelName))
				&& (!localName.replace('-','_').equals(objectName))&& (!localName.replace('-','_').equals(nestedModelName))) {
			if (parsingNestedObject) {
				parsedNestedObject.setFieldName(localName.replace('-', '_'));
				this.field = true;
			} else {
				parsedObject.setFieldName(localName.replace('-', '_'));
				this.field = true;				
			}
			value = "";
		}
	}

	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.replace('-','_').equals(objectName))		
			parsedObject.syncObject(modelName, fieldNames);			
		else if (localName.replace('-','_').equals(nestedObjectName)) {
			if (modelName.equals("streams")) {
				parsedNestedObject.setFieldName("stream_id");
				parsedNestedObject.addValue(parsedObject.getID());							
			}
			parsingNestedObject=false;
			parsedNestedObject.syncObject(nestedModelName, nestedFieldNames);
		} else if (field) {
			if (parsingNestedObject)
				parsedNestedObject.addValue(value);
			else
				parsedObject.addValue(value);
			value = "";
			field = false;
		}
	}

	@Override
	public void characters(char ch[], int start, int length) {
		value = new String(ch, start, length);
	}

}
